//
//  AppDelegate.swift
//  AnneKit-Swift
//
//  Created by 宁小陌 on 2021/2/2.
//  Copyright © 2021 宁小陌. All rights reserved.
//

import UIKit
import IQKeyboardManager

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = UIColor.white
        window?.rootViewController = NXMRootViewController()
        window?.makeKeyAndVisible()
        
        //控制点击背景是否收起键盘
        IQKeyboardManager.shared().shouldResignOnTouchOutside = true
        TestForIn()
        return true
    }
    
    func TestForIn() {
        let list = ["A","B","C","D"];
        for item in list.enumerated() {
            print("idx =\(item)");
        }
        
        for (idx,item) in list.enumerated() {
            print("下标：\(idx) 值：\(item)")
        }
        
        for idx in list.reversed() {
            print("idx =\(idx)");
        }
        
        for idx in stride(from: 0, to: 10, by: 2) {
            print("idx = \(idx)");
        }
        
        let listArr = NSMutableArray()
        let dic = NSMutableDictionary()
        dic.setValue("zhouyu", forKey: "title")
        dic.setValue("huanggai", forKey: "name")
        dic.setValue("zhangliang", forKey: "sub")
        dic.setValue("liubang", forKey: "mark")
        listArr.add(dic)
        
        NXMUILog(parameter: dic["title"])
        
        let dic1 = NSMutableDictionary()
        dic1.setValue("周瑜", forKey: "title")
        dic1.setValue("黄盖", forKey: "name")
        dic1.setValue("张良", forKey: "sub")
        dic1.setValue("刘邦", forKey: "mark")
        listArr.add(dic1)
        for (idx,item) in listArr.enumerated() {
            print("下标：\(idx) 值：\(item)")
        }
        
        var lis:[[String:Any]] = []
        let v1 = ["name":"微信"]
        let v2 = ["name":"QQ"]
        lis.append(v1)
        lis.append(v2)
        NXMUILog(parameter: lis)
        for (index, value) in lis.enumerated() {
            NXMUILog(parameter: value["name"] as! String + "\(index)")
        }
        
        
        
        let totalAmount = Double(1055000)/100
        switch totalAmount {
        case (0..<10000):
            print("0到1万")
        //                    let result: String = String(format:"￥%.2f",totalAmount)
            let result: String = String(format:"￥%.2f",(Double(totalAmount)))
            if let _range = result.range(of : ".") {
                let query = String(result[..<_range.lowerBound])
        //                formatMutableAttr(result: query)
                print("\(query)万")
            }else{
                print("\(result)万")
            }
            break
        case (10000..<1000000):
                print("1万到10万")
            let totalResult = Double(totalAmount) / 10000.0
        //                    let result: String = String(format:"￥%.2f",totalResult)
        //                    let i = lroundf(Float(totalResult))
        //                    print("\(result)万")
        //                    print("￥\(i)万")
            let rs = String(format:"%.2f",totalResult)
            let result: String = String(format:"￥\(lroundf(Float(rs)!))万")
            print("\(result)")
            print("￥\(Double(rs)!.roundTo(places: 2))万")
            print("￥\(lroundf(Float(Double(rs)!.truncate(places: 2))))万")
            print("￥\(Double(rs)!.rounded())万")
            
            break
        case (1000000..<20000000):
            print("10万到100万")
            let totalResult = Double(totalAmount) / 10000.0
        //                    let i = lroundf(Float(totalResult))
        //                    print("￥\(i)万")
            let rs = String(format:"%.2f",totalResult)
            let result: String = String(format:"￥\(lroundf(Float(rs)!))万")
            print("\(result)")
            break
        default:
            print("大于1000")
            break
        }
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

extension Double {
    ///四舍五入 到小数点后某一位
    func roundTo(places: Int) -> Double {
        let divisor = pow(10.0,Double(places))
        return (self * divisor).rounded() / divisor
    }
    ///截断 到小数点后某一位
    func truncate(places: Int) -> Double {
        let divisor = pow(10.0, Double(places))
        return Double(Int(self * divisor)) / divisor
    }
}
